# app/utils/common.py
import os
from ..config.config import Config


class Common:
    def __init__(self):
        self.config = Config()

    def build_data(self):
        """构造数据集：返回 datas 与 word2id"""
        datas = []
        sample_x, sample_y = [], []
        vocab_list = ["PAD", "UNK"]

        with open(self.config.train_txt_path, "r", encoding="utf-8") as f:
            for line in f:
                line = line.strip().split("\t")
                if len(line) != 2:
                    continue
                char, cate = line[0], line[1]
                sample_x.append(char)
                sample_y.append(cate)
                if char not in vocab_list:
                    vocab_list.append(char)

                # 按结束符号切分句子
                if char in {"?", "!", "。", "？", "！"}:
                    datas.append([sample_x, sample_y])
                    sample_x, sample_y = [], []

        word2id = {word: idx for idx, word in enumerate(vocab_list)}
        self.write_file(vocab_list, self.config.vocab_txt_path)
        return datas, word2id

    def write_file(self, vocab_list, file_path):
        """保存词表文件 vocab.txt"""
        with open(file_path, "w", encoding="utf-8") as f:
            f.write("\n".join(vocab_list))